import {getActivityFilter} from "../../api/activity";
import Util from "../../utils/util";

const app = getApp();
Component({
  properties: {
    filterDialog: {
      type: Boolean,
      value: false,
      observer: function (newVal, oldVal) {
        if (newVal) {
          this.initData()
        }
      }
    },
    member: {
      type: Object,
      observer: function (newVal, oldVal) {
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    filterFields: []
  },

  attached: function () {
  },

  /**
   * 组件的方法列表
   */
  methods: {
    initData(e) {
      if (Util.isEmpty(this.data.filterFields)) {
        getActivityFilter().then((res) => {
          this.setData({
            filterFields: res.data.filterData || null
          })
        })
      }
    },

    closeLayer(e) {
      this.setData({
        filterDialog: false,
      })
      this.triggerEvent('closeFilterDialog', false)
    },

    bindChange(e) {
      const id = e.target.id
      const fields = this.data.filterFields;
      if (!e.target.id) {
        return;
      }
      fields[e.target.id].default_val = e.detail.value;
      this.setData({
        filterFields: fields
      })
    },

    changeRadio(e) {
      let fields = this.data.filterFields
      fields[e.currentTarget.dataset.key].default_val = e.currentTarget.dataset.v;

      this.setData({
        filterFields: fields
      })
    },

    checkboxChange(e) {
      let fields = this.data.filterFields;
      if (!e.target.id) {
        return;
      }
      let values = e.detail.value;
      fields[e.target.id].values = values;
      for (const k in values) {
        values[k] = parseInt(values[k]);
      }
      fields[e.target.id].options.map(v => {
        if (values.indexOf(parseInt(v.id)) != -1) {
          v.checked = true;
        } else {
          v.checked = false;
        }
      })

      this.setData({filterFields: fields})
    },

    formSubmit(e) {
      const that = this;
      let formData = {}
      let filterFields = this.data.filterFields;

      for (const k in filterFields) {
        if (filterFields[k].type == 'checkbox') {
          if (filterFields[k].values && filterFields[k].values.length > 0) {
            formData[filterFields[k].name] = filterFields[k].values
          }
          continue;
        } else if (filterFields[k].type == 'radio') {
          if (filterFields[k].default_val >= 0) {
            formData[filterFields[k].name] = filterFields[k].options[parseInt(filterFields[k].default_val)].id;
            continue;
          }
        }
      }
      this.setData({filterDialog: false, filterFields: filterFields})
      this.triggerEvent("confirmFilter", formData || {});
    },

    formReset() {
      let filterFields = this.data.filterFields;

      for (const k in filterFields) {
        if (filterFields[k].type == 'checkbox') {
          filterFields[k].options.forEach(v => {
            v.checked = false;
          })
          continue;
        }
        filterFields[k].default_val = -1;
      }
      this.setData({
        filterFields: filterFields
      })
      // this.triggerEvent("resetFilter");
    }
  }

});
